#! /usr/bin/env python
# coding=utf-8
# 使用cut adaptor 对 reads 进行剪切
tmp_file = r'cut_adaptor_tmp'




import time
import os
import shutil
import argparse
import sys
import threading





parser = argparse.ArgumentParser(
    description=''' 使用cutadapt 去除 barcode 序列的引物 
    这个程序在barcode_assign_to_file.py运行完成后 对result 运行
    用法:
    run_cut_adaptor_trim.py -t 48 -i result -l ATTCAACCAATCATAAAGATATTGG -r TGATTTTTTGGACATCCAGAAGTTTA -o cutprimer
    由大天才于2022年2月17日创建于浙江农业大学''')

parser.add_argument('-i',
                help='run_cut_adaptor_trim.py 输出的存放着各个标本barcode fastq的 文件')

parser.add_argument('-l',
                help='5p引物序列')
parser.add_argument('-r',
                help='3p引物序列')

parser.add_argument('-t',
                help='并行线程数')

parser.add_argument('-o',
                help='输出的目录')

args = parser.parse_args()


if not args.i or not args.o or not args.l or not args.r:
    parser.print_help()
    sys.exit()




infile = args.i

outfile = args.o

left_primer = args.l #r'ATTCAACCAATCATAAAGATATTGG'

right_primer = args.r #r'TGATTTTTTGGACATCCAGAAGTTTA'

threads = int(args.t)

try:
	shutil.rmtree(tmp_file)
except:
	pass
try:
	os.mkdir(tmp_file)
except:
	pass

try:
	shutil.rmtree(outfile)
except:
	pass
try:
	os.mkdir(outfile)
except:
	pass


index = 0


thread_pool = []


def run_func(left_primer,right_primer,infile,tmp_file, outfile,target_file):

	global index
	global core

	index = index + 1
	# 运行 cutadaptor
	#print('cutadapt -g %s --quiet --trimmed-only  -o %s %s' %(left_primer, tmp_file+'/'+target_file, infile +'/'+target_file))
	#print('cutadapt -a %s --quiet --trimmed-only  -o %s %s' %(right_primer, outfile+'/'+target_file, tmp_file+'/'+target_file))
	a = os.system('cutadapt -g %s --quiet --trimmed-only  -o %s %s' %(left_primer, tmp_file+'/'+target_file, infile +'/'+target_file))
	#print('cutadapt -g %s  --trimmed-only  -o %s %s' %(left_primer, tmp_file+'/'+i, infile +'/'+i))
	if a ==0:
		os.system('cutadapt -a %s --quiet --trimmed-only  -o %s %s' %(right_primer, outfile+'/'+target_file, tmp_file+'/'+target_file))
	else:
		print('error at' % target_file)
	#print('cutadapt -a %s  --trimmed-only  -o %s %s' %(right_primer, outfile+'/'+i, tmp_file+'/'+i))
	

	if index >= len(thread_pool):
		return None
	else:
		thread_pool[index].start()

	return	None

n = 0
for i in os.listdir(infile):
	if len(i)>1 and i[0].strip()!='.':
		# # 运行 cutadaptor
		# a = os.system('cutadapt -g %s --quiet --trimmed-only  -o %s %s' %(left_primer, tmp_file+'/'+i, infile +'/'+i))
		# #print('cutadapt -g %s  --trimmed-only  -o %s %s' %(left_primer, tmp_file+'/'+i, infile +'/'+i))
		
		# b = os.system('cutadapt -a %s --quiet --trimmed-only  -o %s %s' %(right_primer, outfile+'/'+i, tmp_file+'/'+i))
		# #print('cutadapt -a %s  --trimmed-only  -o %s %s' %(right_primer, outfile+'/'+i, tmp_file+'/'+i))
		n += 1
		th = threading.Thread(target=run_func, args=[left_primer, right_primer, infile, tmp_file, outfile,i], name='th_'+str(n))
		thread_pool.append(th)



for i in range(threads):
	if len(thread_pool)>i:
		thread_pool[i].start()
